<template>
  <div>
    <el-table :data="devices" style="width: 100%">
      <el-table-column prop="ip" label="ip" width="180"></el-table-column>
      <el-table-column prop="online" label="状态" width="280">
        <template slot-scope="scope">
          <el-button type="text" size="small" disabled>
            {{ scope.row.online ? "在线" : "离线" }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="brightness" label="屏幕亮度" width="180">
      </el-table-column>
      <el-table-column prop="volumn" label="音量">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.volumn"
            size="small"
            @change="changevolumn(scope.row)"
          >
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="pushdata" label="书籍数据同步"
        ><template slot-scope="scope">
          <el-button
            @click.native.prevent="Pushdata(scope.$index, scope.row)"
            type="text"
            size="small"
          >
            推送
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="reboot" label="操作"
        ><template slot-scope="scope">
          <el-button
            @click.native.prevent="Reboot(scope.$index, scope.row)"
            type="text"
            size="small"
          >
            重启
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// @ is an alias to /src
import { listdevice, reboot, setvolumn, pushdata } from "@/request/device_api.js";
// const { rootUrl } = require("/public/config.json");
export default {
  name: "ShelfBook",
  components: {
    // HelloWorld
  },
  data() {
    return {
      devices: []
    };
  },
  computed: {},
  mounted() {
    listdevice().then(result => {
      console.log(result.data);
      this.devices = result.data;
      console.log(this.devices, this.devices.length);
    });
  },
  methods: {
    // 编辑书籍
    Reboot(index, row) {
      console.log(index, row);
      this.$confirm("确定重启设备?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          reboot(row).then(result => {
            console.log(result);
            this.$message({
              type: "success",
              message: "重启指令已发送!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消重启"
          });
        });
    },
    Pushdata(index, row) {
      console.log(index, row);
      this.$confirm("确定推送数据给设备?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          pushdata(row).then(result => {
            console.log(result);
            this.$message({
              type: "success",
              message: "书籍数据推送成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    changevolumn(row) {
      console.log("音量改变", row);
      setvolumn(row).then(result => {
        console.log(result.data);
      });
    }
  }
};
</script>

<style scoped lang="scss">
.devicelist {
  display: flex;
  flex-direction: column;
}
.deviceitem {
  display: flex;
  flex-direction: row;
}
.online {
  background: green;
  width: 2em;
  height: 2em;
  border-radius: 1em;
}
</style>
